package com.pen.interfece.service.Impl;

import com.alibaba.fastjson.JSON;
import com.pen.interfece.Context.BaseContext;
import com.pen.interfece.mapper.MaintenanceMapper;
import com.pen.interfece.mapper.UserMapper;
import com.pen.interfece.pojo.entity.Maintenance;
import com.pen.interfece.server.WebSocketServer;
import com.pen.interfece.service.MaintenanceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
public class MaintenanceServiceImpl implements MaintenanceService {

    @Autowired
    private MaintenanceMapper maintenanceMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private WebSocketServer webSocketServer;
    @Override
    public void addmaintenance(Maintenance maintenance) {
        Long id=BaseContext.getCurrentId();//查出来的是账号的id，此时要把账号中的学生id解析出来
        String account=userMapper.pageQuery(id);
        // 找到 '@' 符号的位置
        int atIndex = account.indexOf('@');
        // 截取 '@' 符号后面的部分
        String result = account.substring(atIndex + 1);
        int StuId= Integer.parseInt(result);
        log.info(String.valueOf(StuId));

        maintenance.setStudentId(StuId);
        maintenanceMapper.addmaintenance(maintenance);

        Object s="您有新的报修项目了，请尽快处理";
        //转化为json格式
        String json = JSON.toJSONString(s);
        //通过WebSocket实现来单提醒，向客户端浏览器推送消息(调用WebSocketServer类中群发的方法)
        webSocketServer.sendToAllClient(json);

    }
}
